﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using nwrole.toolbox.sync.ServiceHost;
using nwrole.toolbox.sync.entity;
using nwrole.toolbox.sync.client;
using sync_web2.Utils;

namespace sync_web2.Controllers
{
    public class EntityController : Controller
    {
        public ActionResult Show(long id)
        {
            //var e = (new SyncEntityDemoRepository()).GetSyncEntity();

            SyncSourceServiceClient client = GetSourceServiceClient();
            var e = client.GetEntityById(id);
            return View(e);
        }

        public ActionResult SchemaShow(long id, short type)
        {
            //var e = (new SyncEntityDemoRepository()).GetSyncEntity();

            SyncSourceServiceClient client = GetSourceServiceClient();
            var e = client.GetSchemaEntity(id, type);
            return View(e);
        }

        public ActionResult ToggleSyncEnabled(long id)
        {
            SyncSourceServiceClient client = GetSourceServiceClient();
            var result = client.ToggleSyncEnabled(id);
            return Json(new
            {
                newValue = result,
                success = true
            }, JsonRequestBehavior.AllowGet);

            /*var val = SyncEntityColumn.ToggleSyncEnabled(id);
            return Json(new
            {
                newValue = val,
                success = true
            }, JsonRequestBehavior.AllowGet);*/
        }

        public ActionResult ToggleSyncMode(long id)
        {
            SyncSourceServiceClient client = GetSourceServiceClient();
            var result = client.ToggleSyncMode(id);
            return Json(new
            {
                newValue = result.ToString(),
                success = true
            }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult SyncOnce(long id)
        {
            SyncSourceServiceClient client = GetSourceServiceClient();
            // // client.SyncOnce(id);

            return Json(new
            {
                newValue = 1,
                success = true
            }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult ImportSchema(long id, short type)
        {
            SyncSourceServiceClient client = GetSourceServiceClient();
            // // client.ImportSchema(

            return Json(new
            {
                newValue = 1,
                success = true
            }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult DeploySource(long id)
        {
            SyncSourceServiceClient client = GetSourceServiceClient();
            client.DeploySource(id);

            return Json(new
            {
                newValue = 1,
                success = true
            }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult DeployDestination(long id)
        {
            SyncSourceServiceClient srcClient = GetSourceServiceClient();
            //srcClient.DeployDestination(id);
            SyncEntity syncEntity = srcClient.GetEntityById(id);

            SyncDestinationServiceClient destClient = GetDestinationServiceClient();
            if (destClient.DeployDestination(syncEntity))
            {
                syncEntity.DeployDestinationDat = DateTime.Now;
                srcClient.Update(syncEntity);
            }            
            
            return Json(new
            {
                newValue = 1,
                success = true
            }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult DeleteSyncEntity(long id)
        {
            SyncSourceServiceClient client = GetSourceServiceClient();
            client.DeleteSyncEntity(id);

            return Json(new
            {
                newValue = 1,
                success = true
            }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult DeploySourceEnvironment()
        {
            SyncSourceServiceClient client = GetSourceServiceClient();
            client.DeploySourceEnvironment();

            return Json(new
            {
                newValue = 1,
                success = true
            }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult DeployDestEnvironment()
        {
            SyncDestinationServiceClient destClient = GetDestinationServiceClient();
            destClient.DeployDestEnvironment();

            return Json(new
            {
                newValue = 1,
                success = true
            }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult ToggleColumnSyncEnabled(long id)
        {
            SyncSourceServiceClient client = GetSourceServiceClient();
            var result = client.ToggleSyncColumnEnabled(id);
            return Json(new
            {
                newValue = result,
                success = true
            }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult DemoToggleColumnSyncEnabled(long id)
        {
            var val = SyncEntityColumn.ToggleSyncEnabled(id * 100);
            return Json(new
            {
                newValue = val,
                success = true
            }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Index()
        {
            SyncSourceServiceClient client = GetSourceServiceClient();
            List<SyncEntity> list = null; //  client.GetEntities();


            if(list == null)
                list = new List<SyncEntity>(); // Create an empty list for the view's sake

            // var list = (new SyncEntityDemoRepository()).GetSyncEntityList();
            return View(list);
        }

        public ActionResult SchemaIndex()
        {
            SyncSourceServiceClient client = GetSourceServiceClient();
            var list = client.GetSchemaEntities();

            return View(list);
        }

        public ActionResult Errors()
        {
            return View();
        }

        private SyncSourceServiceClient GetSourceServiceClient()
        {
            /*bool runningSyncSourceLocal;
            if (!bool.TryParse(ConfigurationManager.AppSettings["RunningSyncSourceLocal"], out runningSyncSourceLocal))
                runningSyncSourceLocal = false;

            if (runningSyncSourceLocal)
            {
                string localServicePath = ConfigurationManager.AppSettings["SyncSourceLocalServicePath"];
                return new SyncSourceServiceClient(localServicePath);
            }

            string sbServiceNamespace = ConfigurationManager.AppSettings["SyncSourceSBServiceNamespace"];
            string sbServicePath = ConfigurationManager.AppSettings["SyncSourceSBServicePath"];
            string sbIssuerName = ConfigurationManager.AppSettings["SyncSourceSBIssuerName"];
            string sbIssuerSecret = ConfigurationManager.AppSettings["SyncSourceSBIssuerSecret"];

            return new SyncSourceServiceClient(sbServiceNamespace, sbServicePath, sbIssuerName, sbIssuerSecret);*/

            return new SyncSourceServiceClient(SiteSettings.SyncSourceConnInfo);
        }

        private SyncDestinationServiceClient GetDestinationServiceClient()
        {
            return new SyncDestinationServiceClient(SiteSettings.SyncDestinationConnInfo);
        }

        
    }
}
